
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
@sapphire/snowflake
Advanced tools
@sapphire/snowflake is a utility library for generating and parsing unique snowflake IDs, which are often used in distributed systems to ensure unique identifiers. It is particularly useful in applications that require high-scale, unique ID generation.
Generate Snowflake ID
This feature allows you to generate a unique snowflake ID. The generated ID is a 64-bit integer that is unique and can be used as an identifier in distributed systems.
const { Snowflake } = require('@sapphire/snowflake');
const snowflake = new Snowflake();
const id = snowflake.generate();
console.log(id);
Parse Snowflake ID
This feature allows you to parse a snowflake ID to extract its components, such as the timestamp, worker ID, and process ID. This can be useful for debugging or analyzing the IDs.
const { Snowflake } = require('@sapphire/snowflake');
const snowflake = new Snowflake();
const id = snowflake.generate();
const parsed = snowflake.deconstruct(id);
console.log(parsed);
Custom Epoch
This feature allows you to set a custom epoch for the snowflake ID generation. The epoch is the starting point for the timestamp component of the ID, and setting a custom epoch can be useful for aligning IDs with specific events or timeframes.
const { Snowflake } = require('@sapphire/snowflake');
const customEpoch = 1609459200000; // Custom epoch (e.g., January 1, 2021)
const snowflake = new Snowflake(customEpoch);
const id = snowflake.generate();
console.log(id);
The 'snowflake-id' package is another library for generating unique snowflake IDs. It offers similar functionality to @sapphire/snowflake, including ID generation and parsing. However, it may have different performance characteristics and configuration options.
The 'flake-idgen' package is a lightweight library for generating unique IDs using the snowflake algorithm. It is designed to be fast and efficient, making it a good alternative to @sapphire/snowflake for applications that require high-throughput ID generation.
The 'node-snowflake' package is a simple implementation of the snowflake ID generation algorithm. It provides basic functionality for generating and parsing snowflake IDs, similar to @sapphire/snowflake, but may lack some of the advanced features and customization options.
Table of Contents
There is often a need to get a unique ID for entities, be that for Discord messages/channels/servers, keys in a database or many other similar examples. There are many ways to get such a unique ID, and one of those is using a so-called "snowflake". You can read more about snowflake IDs in this Medium article.
You can use the following command to install this package, or replace npm install
with your package manager of choice.
npm install @sapphire/snowflake
Note: While this section uses require
, the imports match 1:1 with ESM imports. For example const { Snowflake } = require('@sapphire/snowflake')
equals import { Snowflake } from '@sapphire/snowflake'
.
// Import the Snowflake class
const { Snowflake } = require('@sapphire/snowflake');
// Define a custom epoch
const epoch = new Date('2000-01-01T00:00:00.000Z');
// Create an instance of Snowflake
const snowflake = new Snowflake(epoch);
// Generate a snowflake with the given epoch
const uniqueId = snowflake.generate();
// Import the Snowflake class
const { DiscordSnowflake } = require('@sapphire/snowflake');
// Generate a snowflake with Discord's epoch
const uniqueId = DiscordSnowflake.generate();
// Alternatively, you can use the method directly
const uniqueId = DiscordSnowflake.generate();
// Import the Snowflake class
const { TwitterSnowflake } = require('@sapphire/snowflake');
// Generate a snowflake with Twitter's epoch
const uniqueId = TwitterSnowflake.generate();
// Alternatively, you can use the method directly
const uniqueId = TwitterSnowflake.generate();
// Import the Snowflake class
const { Snowflake } = require('@sapphire/snowflake');
// Define a custom epoch
const epoch = new Date('2000-01-01T00:00:00.000Z');
// Create an instance of Snowflake
const snowflake = new Snowflake(epoch);
// Deconstruct a snowflake with the given epoch
const uniqueId = snowflake.deconstruct('3971046231244935168');
// Import the Snowflake class
const { DiscordSnowflake } = require('@sapphire/snowflake');
// Deconstruct a snowflake with Discord's epoch
const uniqueId = DiscordSnowflake.deconstruct('3971046231244935168');
// Alternatively, you can use the method directly
const uniqueId = DiscordSnowflake.deconstruct('3971046231244935168');
// Import the Snowflake class
const { TwitterSnowflake } = require('@sapphire/snowflake');
// Deconstruct a snowflake with Twitter's epoch
const uniqueId = TwitterSnowflake.deconstruct('3971046231244935168');
// Alternatively, you can use the method directly
const uniqueId = TwitterSnowflake.deconstruct('3971046231244935168');
Sapphire Community is and always will be open source, even if we don't get donations. That being said, we know there are amazing people who may still want to donate just to show their appreciation. Thank you very much in advance!
We accept donations through Open Collective, Ko-fi, PayPal, Patreon and GitHub Sponsorships. You can use the buttons below to donate through your method of choice.
Donate With | Address |
---|---|
Open Collective | Click Here |
Ko-fi | Click Here |
Patreon | Click Here |
PayPal | Click Here |
Please make sure to read the Contributing Guide before making a pull request.
Thank you to all the people who already contributed to Sapphire!
FAQs
Deconstructs and generates snowflake IDs using BigInts
We found that @sapphire/snowflake demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.